package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Yan
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @Override
    @Transactional
    public Result<ShoppingCart> addShoppingCart(ShoppingCart shoppingCart) {

        if (ObjectUtils.isEmpty(shoppingCart)){
            return Result.error("数据错误");
        }
        //2.核心逻辑（1.获取购物车用户，2.判断添加菜品还是套餐 3.保存数据库）
        //设定购物车当前用户Id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //查询当前菜品或套餐是否在购物车里

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);

        if (ObjectUtils.isEmpty(dishId)){
            //添加的是套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        }else {
            //添加的是菜品
            wrapper.eq(ShoppingCart::getDishId,dishId);
        }
        //在购物车数据库中查看，是否添加过
        ShoppingCart one = this.getOne(wrapper);
        if (ObjectUtils.isEmpty(one)){
            //不存在，添加到购物车中
            //BeanUtils.copyProperties(shoppingCart,one);
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //this.save(shoppingCart);
            one = shoppingCart;
        }else {
            //已存在，在将原来的number+1
            Integer number = one.getNumber();
            one.setNumber(number+1);
            //this.updateById(one);
        }
        this.saveOrUpdate(one);

        return Result.success(one);
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public Result<List<ShoppingCart>> shoppingCartList() {

        if (ObjectUtils.isEmpty(BaseContext.getCurrentId())){
            return Result.error("登录过期");
        }

        LambdaQueryWrapper<ShoppingCart> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        wrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = this.list(wrapper);

        return Result.success(list);
    }

    /**
     * 清空回收站
     * @return
     */
    @Override
    public Result<String> clean() {
        //校验登录是否过期
        if (ObjectUtils.isEmpty(BaseContext.getCurrentId())){
            return Result.error("登录过期");
        }
        LambdaQueryWrapper<ShoppingCart> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        this.remove(wrapper);
        return Result.success("清空购物车成功");
    }

    /**
     * 修改商品数量，-1
     * @param shoppingCart
     * @return
     */
    @Override
    public Result<ShoppingCart> sub(ShoppingCart shoppingCart) {
        if (ObjectUtils.isEmpty(shoppingCart)){
            return Result.error("数据错误");
        }

        //查询当前用户购物车数据
        LambdaQueryWrapper<ShoppingCart> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());


        Long dishId = shoppingCart.getDishId();
        if (ObjectUtils.isEmpty(dishId)){
            //修改的是套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        }else {
            //修改的是菜品
            wrapper.eq(ShoppingCart::getDishId,dishId);
        }

        ShoppingCart one = this.getOne(wrapper);

        Integer number = one.getNumber();

        one.setNumber(number -1);
        this.updateById(one);

        if (number == 1){
            this.removeById(one.getId());
        }
        return Result.success(one);
    }
}